
function createStory() {
	var promptid = document.getElementById("prompt_id");
	var title = document.getElementById("story_title").value;
	var posttext = document.getElementById("post_text");
	var text = posttext.innerHTML;
	
	var tagList = "";
	var count = 0;
	while( document.getElementById( "tag" + count ) != null ) {
		var element = document.getElementById( "tag" + count );
		if( element.checked == true ) {
			if( tagList == "" ) {
				tagList += element.value;
			} else {
				tagList = tagList + "," + element.value;
			}
		}
		count++;
	}
	
	
	if( promptid ) {
		$.getJSON("/writeonproject/create?object=story&promptid=" + promptid.value + "&title=" + title + "&post=" + text + "&tags=" + tagList, createStoryResult );
	} else {
		$.getJSON("/writeonproject/create?object=story&title=" + title + "&post=" + text + "&tags=" + tagList, createStoryResult );
	}
}

function createStoryResult(data) {
	if( data.error == "false" ) {
		window.location	= "/writeonproject/home?page=story_view&storyid=" + data.storyid;
	} else {
		alert("Error! : " + data.reason );
	}
}

function createPrompt() {
	var imgurlfield = document.getElementById("img_url");
	var promptText = document.getElementById("post_text").innerHTML;
	if( imgurlfield ) {
		var imageURL = imgurlfield.value;
		$.getJSON("/writeonproject/create?object=prompt&imgurl=" + imageURL + "&text=" + promptText, createPromptResult);
	} else {
		$.getJSON("/writeonproject/create?object=prompt&text=" + promptText, createPromptResult);
	}
}

function createPromptResult(data) {
	if( data.error == "false" ) {
		window.location = "/writeonproject/home?page=browse_prompts";
	} else {
		alert("Error! : " + data.reason );
	}
}

function createUserInfoPage() {
	var uid = document.getElementById("uidfield").value;
	$.getJSON("/writeonproject/update?page=user&userid=" + uid, createUserCallBack );
}

function createUserCallBack(data) {
	var divspace = document.getElementById("userinfo");
	if( data != null && data.error != null & data.error != "true" ) {
		divspace.innerHTML += "<h2>Profile: " + data.userid + "</h2>";
		divspace.innerHTML += "<b>Name:</b> " + data.fname + " " + data.lname + "<br>";
		divspace.innerHTML += "<b>Gender:</b> " + data.gender + "<br>" ;
		divspace.innerHTML += "<b>Location:</b> " + data.location + "<br>" ;
		divspace.innerHTML += "<b>Last Spotted:</b> " + data.lastupdate + "<br>";
		divspace.innerHTML += "<b>Writing Since:</b> " + data.datecreate + "<br>";
		divspace.innerHTML += "<a href=\"/writeonproject/home?page=storylists&userId=" + data.userid + "\"> [ View Storylists ] </a><br><br>";
		divspace.innerHTML += "<b>About Me:</b> " + data.biography ;
		divspace.innerHTML += "<br><br><b> User Contributed to Stories:</b><br>";
		
		if( data.stories.length > 0 ) {
			for( var i = 0; i < data.stories.length; i++ ) {
				var story = data.stories[i];
				divspace.innerHTML += "<a href=\"/writeonproject/home?page=story_view&storyid=" + story.storyid + "\"> " + story.storytitle + "</a><br>"; 
			}
		}
	} else {
		divspace.innerHTML = "Error: Could not find the profile for the requested user";
	}
	
}

function initializePromptBox(promptid) {
	if (promptid != null && promptid != "") {
		$.getJSON("/writeonproject/update?page=get_prompt&promptid=" + promptid, initializePromptBoxCallBack);	
	}
}

function initializePromptBoxCallBack(data) {
	if( data.error != null && data.error == "false" ) {
		var prompt_box = document.getElementById("prompt_box");
		
		if (prompt_box != null && data.promptid != null) {
			var row = "<tr> <td class=\"post_col1\">Prompt</td>";
			row += "<input type=\"hidden\" id=\"prompt_id\" value=\"" + data.promptid + "\"/>";
			row += "<td><div class=\"post_field\" id=\"prompt_info\">";
			
			var filepath = data.imagefilepath;
			if (filepath != null && filepath != "") {
				row += "<img src=\"" + filepath + "\" width=\"50%\" height=\"50%\" class=\"img_preview\"></br>";
			}
			
			var text = data.textprompt;
			if (text != null && text != "") {
				row += text;
			}
			
			row += "</div></td></tr>";
			prompt_box.innerHTML = row;
		}
	} else {
		alert("Error! : " + data.reason );
	}
}
